package feign.ethan.springCloud.controller;

import feign.ethan.springCloud.pojo.Dept;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Ethan
 */
@RestController
@Slf4j
public class DeptConsumerController {

    //引入ribbon之前,通过服务提供者的url调用
//    public static final String URI ="http://127.0.0.1:8001/dept";
    /**
     * ribbon的引入,去eureka的注册中心去拿url
     */
    public static final String URI ="http://SPRINGCLOUD-PROVIDER-DEPT/dept";

    /**
     * //理解:消费者 不应该有service //用RestTemplate 封装的方法调服务 //restTemplate(url , 实体:map , Class<T> responseType )
     */
    @Resource
    private RestTemplate restTemplate;

    @RequestMapping("/consumer/other/dept/detail/{id}")
    public Dept detail(@PathVariable("id") Integer id) {
        Dept dept = restTemplate.getForObject(URI + "/detail/" + id, Dept.class);
        log.info("dept==>{}", dept);
        return dept;
    }

    @RequestMapping("/consumer/other/dept/save")
    public Boolean save(Dept dept) {
        log.info("==>>{}",dept);
        Dept deptNew = new Dept().setName(dept.getName());
        return restTemplate.postForObject(URI + "/insert", deptNew, Boolean.class);
    }

    @RequestMapping("/consumer/other/all")
    public List<Dept> all() {
        return restTemplate.getForObject(URI + "/all" , List.class);
    }

    @RequestMapping("/consumer/other/delete/{id}")
    public void delete(@PathVariable("id") Integer id) {
        restTemplate.delete(URI + "/delete/" + id);
    }

}
